﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class add_diction extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->helper('url'); //You should autoload this one ;)
		$this->load->helper('ckeditor');
		$this->load->library('recaptcha');
		
        $this->data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();
		//Ckeditor's configuration
		$this->data['error'] ='';
		$data['add_word_success'] ="";
		$this->data['ckeditor'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"Full",		//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
 
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 1' => array (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 	=> 	'Blue',
						'font-weight' 	=> 	'bold'
					)
				),
 
				//Creating a new style named "style 2"
				'style 2' => array (
					'name' 	=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 		=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
	}
	
	function index(){
		$key = 'flksjdfkjsldkfljslww3ruwe22AFSAGS';
		$userID = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_COOKIE['ID']), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
		$this->load->Model("auth");
		$check_login = $this->auth->check_login($userID);
		if($check_login)
			$this->load->view('upload_form', $this->data);
		else{
			header('Location:'.Base_url_TuDien.'/auth');
		}
	}
	
	#Edit
	
	
	#add Word with files
	function upload_ctrl()
	{
		try{
		/*
			$this->load->library('recaptcha');
			$this->recaptcha->recaptcha_check_answer();
			*/
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'txt';
			$config['max_size']	= '1024';
			
			$this->load->helper('form');
			$this->load->library('upload', $config);
			$data['error']="";
			$max = $i=0;
			$data['lang'] = "";
			
			if ( ! $this->upload->do_upload())
			{
				$data['error'] = $this->upload->display_errors();
				$this->load->view('upload_form', $data);
			}/*
			else if(!$this->recaptcha->getIsValid()){
				$data['error'] = $this->recaptcha->getError();
				$this->load->view('upload_form', $data);
			}*/
			else
			{
				
				$data = array('upload_data' => $this->upload->data());
				$NameUploadFile = './uploads/'.$data['upload_data']['file_name'];
				$lines = file($NameUploadFile); // gets file in array using new lines character
				
				$check = false;
				foreach($lines as $line)
				{
				   $temparr = explode(':',$line);
				   $word = $temparr[0];
				   $MeanWord = $temparr[1];
				   
					$key = 'flksjdfkjsldkfljslww3ruwe22AFSAGS';
					$userID = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_COOKIE['ID']), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
					$data['lang'] =  $lang = $this->input->post('dict', TRUE);
					
					$data = array(
							   'word' => $word,
							   'MeanWord' => $MeanWord,
							   'userID' => $userID, //ID Username
							   'DateInsert' => date("Y-m-d H:i:s")
							);
						
						//check xem da nap chua
					$this->load->Model("m_search");
					$LikeWord = $this->m_search->search($word,$lang);	
					
					if(count($LikeWord)==0){
						goto next_Dong_Tiep_Theo_Trong_File_Up_Len;
					}
					foreach($LikeWord as $temp){
							if($temp['MeanWord']== $data['MeanWord'])
							{
								goto next_Dong_Tiep_Theo_Trong_File_Up_Len;
							}
						}
					$this->load->Model("function_model");
					$this->function_model->addWord($lang, $data);
					$check=true;
					$i++;
					if($i>$max){
						$max=$i;
					}
					if($i>500){
						break;
					}
				next_Dong_Tiep_Theo_Trong_File_Up_Len:
				}
			unlink($NameUploadFile);
			$data['success'] ="Đã insert thành công : <b>".$max."</b> Word. <br/>Xin Cảm ơn bạn đã đóng góp";
			$data['lang'] =  $lang ;
			$this->load->view("Upload_Success",$data);
			}
			
		}catch(Exception $e){
			$check=false;
		}
	}
	
	public function addWord(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('word', 'username', 'required');
		$this->form_validation->set_rules('MeanWord', 'username', 'required');	
		
		//kiem tra login
		$key = 'flksjdfkjsldkfljslww3ruwe22AFSAGS';
		$userID = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($_COOKIE['ID']), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
		$this->load->Model("auth");
		$check_login = $this->auth->check_login($userID);
		if($check_login == false){
			header('Location:'.Base_url_TuDien.'/auth');
		}
		if($this->input->post('submit')){
			if($this->form_validation->run()){
				$userID = $_COOKIE['ID'];
				$key = 'flksjdfkjsldkfljslww3ruwe22AFSAGS';
				$userID = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($userID), MCRYPT_MODE_CBC, md5(md5($key))), "\0");				
				$data_post = array(
					   'word' => $this->input->post('word', TRUE),
					   'MeanWord' => trim(($this->input->post('MeanWord', TRUE))) ,
					   'userID' => $userID, //ID Username
					   'DateInsert' => date("Y-m-d H:i:s")
					);
				$data['lang'] = $this->input->post('dict', TRUE);
				$this->load->Model("m_search");
				$LikeWord = $this->m_search->search($data_post['word'],$data['lang']);
				foreach($LikeWord as $temp){
						if($temp['MeanWord'] == $data_post['MeanWord'])
						{
							$data = array_merge($data, $data_post);
							$data['add_word_success'] = "Dữ liệu này đã tồn tại trong hệ thống!";
							$data['ckeditor'] = $this->data['ckeditor'];
							$this->load->view("success", $data);
							return;
						}
					}

				$this->load->Model("function_model");
				$this->function_model->addWord($data['lang'], $data_post);
				$data = array_merge($data, $data_post);
				$data['add_word_success'] = "Nạp dữ liệu thành công";
				$data['ckeditor'] = $this->data['ckeditor'];
				$this->load->view("success", $data);
			}
		}else{
				$data['add_word_success']="Sảy ra lỗi, vui lòng thử lại";
				$data['ckeditor'] = $this->data['ckeditor'];
				$this->load->view('function_View', $data);
		}
	
	}
}